package mb.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class AppMenu extends JMenuBar  implements ActionListener {

  private static final int EXIT			= 11;  // &
  private static final int SELECT		= 21;  // &
  private static final int DESELECT	= 22;  // &
  private static final int DELETE		= 23;  // +
  private static final int HIDE			= 31;  // &
  private static final int SHOW			= 32;  // &
  private static final int ABOUT		= 41;  // +

  private JMenu		  menuFile		     = new JMenu("File");
  private JMenuItem menuFileExit	   = new JMenuItem("Exit");
  private JMenu 	  menuEdit 		     = new JMenu("Edit");
  private JMenuItem menuEditSelect 	 = new JMenuItem("Select Boxes");
  private JMenuItem menuEditDeselect = new JMenuItem("Deselect Boxes");
  private JMenuItem menuEditDelete   = new JMenuItem("Delete Selected Boxes"); // +
  private JMenu 	  menuView		     = new JMenu("View");
  private JMenuItem menuViewHide 	   = new JMenuItem("Hide Selected Boxes");
  private JMenuItem menuViewShow 	   = new JMenuItem("Show Selected Boxes");
  private JMenu 	  menuHelp		     = new JMenu("Help");  // +
  private JMenuItem menuHelpAbout 	 = new JMenuItem("About...");  // +

  private AppFrame appFrame;

  public AppMenu() {
  	super();
    menuFileExit.setActionCommand("Exit");
    menuFileExit.addActionListener(this);

    menuEditSelect.setActionCommand("Select");
    menuEditSelect.addActionListener(this);
    menuEditDeselect.setActionCommand("Deselect");
    menuEditDeselect.addActionListener(this);
    menuEditDelete.setActionCommand("Delete"); // +
    menuEditDelete.addActionListener(this); // +

    menuViewHide.setActionCommand("Hide");
    menuViewHide.addActionListener(this);
    menuViewShow.setActionCommand("Show");
    menuViewShow.addActionListener(this);

    menuHelpAbout.setActionCommand("About"); // +
    menuHelpAbout.addActionListener(this); // +

    menuFile.add(menuFileExit);
    menuEdit.add(menuEditSelect);
    menuEdit.add(menuEditDeselect);
    menuEdit.addSeparator(); // +
    menuEdit.add(menuEditDelete); // +
    menuView.add(menuViewHide);
    menuView.add(menuViewShow);
    menuHelp.add(menuHelpAbout); // +

    this.add(menuFile);
    this.add(menuEdit);
    this.add(menuView);
    this.add(menuHelp); // +
	}

  public AppMenu(AppFrame aFrame) {
    this();
    this.setAppFrame(aFrame);
  }

  public AppFrame getAppFrame() {
  	return appFrame;
  }

  public void setAppFrame(AppFrame aFrame) {
  	appFrame = aFrame;
  }

  // {+
  private void displayAbout() {
    AppAbout about = new AppAbout(appFrame);
    Dimension aboutSize = about.getPreferredSize();
    Dimension frameSize = appFrame.getSize();
    Point frameLocation = appFrame.getLocation();
    int x = ((frameSize.width - aboutSize.width) / 2) + frameLocation.x;
    int y = ((frameSize.height - aboutSize.height) / 2) + frameLocation.y;
    about.setLocation(x, y);
    about.setModal(true);
    about.setVisible(true);
  }
  // +}

  private int getCommand(String aCommand) { 
    int	action = -1;
    if      (aCommand.equals("Exit"))     action = EXIT;
    else if (aCommand.equals("Select"))   action = SELECT;
    else if (aCommand.equals("Deselect")) action = DESELECT;
    else if (aCommand.equals("Delete"))   action = DELETE; // +
    else if (aCommand.equals("Hide"))     action = HIDE;
    else if (aCommand.equals("Show"))     action = SHOW;
    else if (aCommand.equals("About"))    action = ABOUT; // +
    return action;
  }

  public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    switch (this.getCommand(command)) { // &
    	case EXIT:      appFrame.exit();
                      break;
    	case SELECT:    appFrame.getDiagram().selectAllBoxes();
                      break;
    	case DESELECT:  appFrame.getDiagram().deSelectAllBoxes();
                      break;
      // {+
      case DELETE:    appFrame.getDiagram().deleteSelectedBoxes();
                      break;
      // +}
    	case HIDE:      appFrame.getDiagram().hideSelectedBoxes();
                      break;
    	case SHOW:      appFrame.getDiagram().showSelectedBoxes();
                      break;
      // {+
      case ABOUT:     this.displayAbout();
                      break;
      // +}
    }
  }

}
